package rexsee.core.utilities;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;

public class RexseeClazz implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Clazz";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return this;
	}

	private final Browser mBrowser;
	public RexseeClazz(Browser browser) {
		mBrowser = browser;
	}

	public static Field getField(Class<?> c, String name) {
		Field[] fields = c.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			if (fields[i].getName().equals(name)) { return fields[i]; }
		}
		return null;
	}
	public static Method getMethod(Class<?> c, String name) {
		Method[] methods = c.getDeclaredMethods();
		for (int i = 0; i < methods.length; i++) {
			if (methods[i].getName().equals(name)) { return methods[i]; }
		}
		return null;
	}
	public static Class<?> getClass(Class<?> c, String name) {
		Class<?>[] classes = c.getClasses();
		for (int i = 0; i < classes.length; i++) {
			if (classes[i].getSimpleName().equals(name)) { return classes[i]; }
		}
		return null;
	}
	public static Object getEnum(Class<?> c, String name) {
		Object[] enums = c.getEnumConstants();
		for (int i = 0; i < enums.length; i++) {
			if (enums[i].toString().equals(name)) return enums[i];
		}
		return null;
	}

	//JavaScriptInterface
	public String getClasses(String className) {
		Class<?> c;
		try {
			c = Class.forName(className);
		} catch (ClassNotFoundException e1) {
			return "The specified class can not be found.";
		}
		String rtn = "";
		try {
			Class<?>[] classes = c.getClasses();
			for (int i = 0; i < classes.length; i++) {
				if (!rtn.equals("")) rtn += ",";
				rtn += classes[i].getSimpleName();
			}
		} catch (Exception e) {
			return "Exception:" + e;
		}
		return rtn;
	}
	public String getDeclaredFields(String className) {
		Class<?> c;
		try {
			c = Class.forName(className);
		} catch (ClassNotFoundException e1) {
			return "The specified class can not be found.";
		}
		String rtn = "";
		try {
			Field[] fields = c.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				if (!rtn.equals("")) rtn += ",";
				rtn += fields[i].getName();
			}
		} catch (Exception e) {
			return "Exception:" + e;
		}
		return rtn;
	}
	public String getDeclaredMethods(String className) {
		Class<?> c;
		try {
			c = Class.forName(className);
		} catch (ClassNotFoundException e1) {
			return "The specified class can not be found.";
		}
		String rtn = "";
		try {
			Method[] methods = c.getDeclaredMethods();
			for (int i = 0; i < methods.length; i++) {
				if (!rtn.equals("")) rtn += ",";
				rtn += methods[i].getName();
			}
		} catch (Exception e) {
			return "Exception:" + e;
		}
		return rtn;
	}
	public String getEnumConstants(String className) {
		Class<?> c;
		try {
			c = Class.forName(className);
		} catch (ClassNotFoundException e1) {
			return "The specified class can not be found.";
		}
		String rtn = "";
		try {
			Object[] enums = c.getEnumConstants();
			if (enums == null) return "";
			for (int i = 0; i < enums.length; i++) {
				if (!rtn.equals("")) rtn += ",";
				rtn += enums[i].getClass().getName() + "|";
				rtn += enums[i].toString();
			}
		} catch (Exception e) {
			return "Exception:" + e;
		}
		return rtn;
	}

}
